<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity5">
<div th:replace="~{include-modal-role-confirm :: roleconfirm}"></div>
<div th:replace="~{include-head :: head}"></div>
<div th:replace="~{include-modal-role-add :: roleadd}"></div>
<div th:replace="~{include-modal-role-edit :: roleedit}"></div>
<link rel="stylesheet" type="text/css" href="/css/pagination.css"/>
<link rel="stylesheet" type="text/css" href="/ztree/zTreeStyle.css">
<body>
<div th:replace="~{include-modal-assign-auth :: roleAssignAuthModal}"></div>
<div th:replace="~{include-nav :: nav}"></div>
<div class="container-fluid">
    <div class="row">
        <div th:replace="~{include-sidebar :: sidebar}"></div>
        <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <h3 class="panel-title"><i class="glyphicon glyphicon-th"></i> 数据列表</h3>
                </div>
                <div class="panel-body">
                    <form class="form-inline" role="form" style="float:left;">
                        <div class="form-group has-feedback">
                            <div class="input-group">
                                <div class="input-group-addon">查询条件</div>
                                <input id="keywordInput" class="form-control has-success" type="text" placeholder="请输入查询条件">
                            </div>
                        </div>
                        <button id="searchBtn" type="button" class="btn btn-warning"><i class="glyphicon glyphicon-search"></i> 查询</button>
                    </form>
                    <button type="button" id="batchRemoveBtn" class="btn btn-danger" style="float:right;margin-left:10px;"><i class=" glyphicon glyphicon-remove"></i> 删除</button>
                    <button id="addBtn" type="button" class="btn btn-primary" style="float:right;">
                        <i class="glyphicon glyphicon-plus"></i> 新增
                    </button><br>
                    <hr style="clear:both;">
                    <div class="table-responsive">
                        <table class="table  table-bordered">
                            <thead>
                            <tr >
                                <th width="30">#</th>
                                <th width="30"><input id="summaryBox" type="checkbox"></th>
                                <th>名称</th>
                                <th width="100">操作</th>
                            </tr>
                            </thead>
                            <tbody id="roleTableBody">
                            <!--ajax加载数据-->
                            </tbody>
                            <tfoot>
                            <tr >
                                <td colspan="4" align="center">
                                    <div id="Pagination" class="pagination">
                                        <!-- 这里显示分页 -->
                                    </div>
                                </td>
                            </tr>
                            </tfoot>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
</html>
<script type="text/javascript" src="/script/jquery.pagination.js"></script>
<script type="text/javascript" src="/ztree/jquery.ztree.all-3.5.min.js"></script>
<script type="text/javascript" src="/script/my-role.js"></script>
<script type="text/javascript">
    //增加
    $("#addBtn").click(function(){
        $("#addModal").modal("show");
    });

    //确认添加
    $("#addModalBtn").click(function () {
        // 1.收集文本框内容
        var roleName = $.trim($("#roleNameInput").val());
        if(roleName == null || roleName == "") {
            layer.msg("请输入有效角色名称！");
            return ;
        }
        $.ajax({
            url:"/role/save/role",
            type:"post",
            data:{
                "roleName":roleName
            },
            dataType:"json",
            success:function (response) {
                var result = response.result;
                if(result == "SUCCESS") {
                    layer.msg("操作成功！");
                    // 前往最后一页
                    window.pageNum = 999999;
                    showPage();
                    // 不管成功还是失败，关闭模态框
                    $("#addModal").modal("hide");

                    // 清理本次在文本框填写的数据
                    $("#roleNameInput").val("");
                }else {
                    layer.msg(response.message);
                }
            }
        });
    });



    //修改
    $("#roleTableBody").on("click",".editBtn",function(){

        // 1.获取当前按钮的roleId
        window.roleId = $(this).attr("roleId");

        // 2.获取当前按钮所在行的roleName
        var roleName = $(this).parents("tr").children("td:eq(2)").text();

        // 3.修改模态框中文本框的value值，目的是在显示roleName
        $("#roleNameInputEdit").val(roleName);

        // 4.打开模态框
        $("#editModal").modal("show");
    });


    //确认修改
    $("#editModalBtn").click(function () {
        // 1.获取文本框值
        var roleName = $.trim($("#roleNameInputEdit").val());

        if(roleName == null || roleName == "") {
            layer.msg("请输入有效角色名称！");

            return ;
        }
        $.ajax({
            url:"/role/update/role",
            type:"post",
            data:{
                "id":window.roleId,
                "name":roleName
            },
            dataType:"json",
            success:function (response) {
                var result = response.result;
                if(result == "SUCCESS") {
                    layer.msg("操作成功！");
                    // 3.操作成功重新分页
                    showPage();
                }
                if(result == "FAILED") {
                    layer.msg(response.message);
                }

                // 4.不管成功还是失败，关闭模态框
                $("#editModal").modal("hide");
            }
        });
    });
</script>

